<template>
  <div
    class="relative h-screen bg-white dark:bg-zinc-800 text-center xl:bg-zinc-200"
  >
    <!-- 头部 -->
    <headerVue />
    <!-- 表单区 -->
    <div
      class="block px-3 mt-4 dark:bg-zinc-800 xl:bg-white xl:w-[388px] xl:dark:bg-zinc-900 xl:m-auto xl:mt-8 xl:py-4 xl:rounded-sm xl:shadow-lg"
    >
      <h3
        class="hidden mb-2 font-semibold text-base text-main dark:text-zinc-300 xl:block"
      >
        账号登录
      </h3>
      <!-- 登录表单 -->
      <vee-form @submit="onLoginHandler">
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-none pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default::bg-zinc-900"
          type="text"
          name="username"
          placeholder="用户名"
          autocomplete="on"
          :rules="validateUsername"
          v-model="loginForm.username"
        />
        <vee-error-message
          name="username"
          class="text-red-600 text-sm block mt-0.5 text-left"
        />
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-none pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default::bg-zinc-900"
          type="password"
          name="password"
          placeholder="密码"
          autocomplete="on"
          :rules="validatePassword"
          v-model="loginForm.password"
        />
        <vee-error-message
          name="password"
          class="text-red-600 text-sm block mt-0.5 text-left"
        />
        <!-- 跳转按钮 -->
        <div class="pt-1 pb-3 leading-[0px] text-right">
          <a
            class="inline-block p-1 text-zinc-400 text-right dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-300 cursor-pointer"
            @click="onToRegister"
          >
            去注册
          </a>
        </div>
        <!-- 登录按钮 -->
        <z-button
          class="w-full dark:bg-zinc-900 xl:dark:bg-zinc-800"
          :isActiveAnim="false"
          :loading="loading"
          >登录</z-button
        >
      </vee-form>
      <!-- 第三方登录 -->
      <div class="flex items-center justify-around mt-4">
        <qq-login></qq-login>
        <weixin-login></weixin-login>
      </div>
    </div>
    <sliderCaptcha
      v-if="isSliderCaptcha"
      @close="onClose"
      @success="onSuccess"
    />
  </div>
</template>

<script setup>
import headerVue from '../components/header.vue'
import {
  Form as VeeForm,
  Field as VeeField,
  ErrorMessage as VeeErrorMessage
} from 'vee-validate'
import { validateUsername, validatePassword } from '../validate'
import sliderCaptcha from './slider-captcha.vue'
import { ref } from 'vue'
import { LOGIN_TYPE } from '@/constants'
import { useUserStore } from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { useAppStore } from '@/store/modules/app'
import qqLogin from './qq-login.vue'
import weixinLogin from './weixin-login.vue'

const userStore = useUserStore()
const appStore = useAppStore()
const router = useRouter()

const isSliderCaptcha = ref(false)

const onClose = () => {
  console.log('关闭')
  isSliderCaptcha.value = false
}

const onSuccess = () => {
  console.log('验证通过')
  isSliderCaptcha.value = false
  // 登录逻辑
  onLogin()
}

/**
 * 登录表单
 */
const onLoginHandler = () => {
  console.log('登录表单')
  isSliderCaptcha.value = true
}

/**
 * 用户登录行为
 */
const loading = ref(false)
const loginForm = ref({
  username: '',
  password: ''
})
const onLogin = async () => {
  loading.value = true
  try {
    await userStore.login({
      ...loginForm.value,
      loginType: LOGIN_TYPE.USERNAME
    })
  } finally {
    loading.value = false
  }
  router.push('/')
}

/**
 * 跳转注册页
 */
const onToRegister = () => {
  appStore.changeRouterType('push')
  router.push('/register')
}
</script>

<style lang="scss" scoped></style>
